package com.zdyt.opc.service.impl;


import com.zdyt.opc.entity.OpcData;
import com.zdyt.opc.entity.OtherData;
import com.zdyt.opc.service.OpcShowDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.*;

@Slf4j
@Service
public class OpcShowDataServiceImpl implements OpcShowDataService {

    private static Map<String, OpcData> opcDatasMap = new ConcurrentHashMap<>();

    private static Map<String, OtherData> otherDatasMap = new ConcurrentHashMap<>();

    static {
        otherDatasMap.put("主值", new OtherData("主值",""));
        otherDatasMap.put("副值", new OtherData("副值",""));
        otherDatasMap.put("计划发电量", new OtherData("计划发电量",""));
        otherDatasMap.put("实际发电量", new OtherData("实际发电量",""));
    }

    //    static int i = 111111111;
//    static {
//        opcDatasMap.put("so2",new OpcData( "so2", i, "二氧化硫1" ,"标题1"));
//        ScheduledExecutorService scheduledExecutorService =
//                Executors.newScheduledThreadPool(10);
//        scheduledExecutorService.scheduleAtFixedRate(()->{
//            i += 1;
//            opcDatasMap.put("so2",new OpcData( "so2", i, "二氧化硫1" ,"标题1"));
//        }, 1, 3 , TimeUnit.SECONDS);
//    }
    public void receiveAndUpdateData(OpcData opcData){
        Object o = opcDatasMap.get(opcData.getItemId());
        if(!opcData.equals(o)){
            opcDatasMap.put(opcData.getItemId(), opcData);
        }
    }

    public List<OpcData> getDataByItemCode(List<String> itemCodes){
//        itemCodes.forEach(e->{
//            System.out.println(e);
//        });
        List<OpcData> result = new ArrayList<>();
        for (String itemCode : itemCodes) {
            OpcData opcData = opcDatasMap.get(itemCode);
            if (Objects.isNull(opcData)){
                continue;
            }
            result.add(opcData);
        }
        return result;
    }

    @Override
    public void updataOtherData(OtherData otherData) {
        otherDatasMap.put(otherData.getName(), otherData);
    }

    @Override
    public List<OtherData> getOtherDatas() {
        Collection<OtherData> values = otherDatasMap.values();
        List<OtherData> otherDatas = new ArrayList<>(values);
        return otherDatas;
    }

    @Override
    public OtherData getOtherDataByKey(String name) {
        return otherDatasMap.get(name);
    }


}
